<?php
$g4_path = "../"; 
require_once ($g4_path.'common.php');

$title = "교재 주문 처리";
$menu = 2;
require_once ('./admin.head.php');
require_once ('./admin.lib.php');

$tpl = new Template_;
$tpl->template_dir = './tpl';
$tpl->compile_dir = './tpl/compile';

$_sqlWhere = $_sqlWhereTemp = $_makeLink = $type = $_type = $keyword = $status = NULL; 
$_tplSetting = $data = array();
$statusArray = array('1'=>'입금대기','2'=>'입금완료','3'=>'배송 중','4'=>'배송 완료','5'=>'신청취소');
$statusKeyArray = array('totalnum'=>'0', 'readynum'=>'1','completenum'=>'2','deliverynum'=>'3','deliverycompletenum'=>'4','canclenum'=>'5'); 
$searchOptionArray = array(NULL=>'검색조건 선택', 'student_name'=>'학생 이름', 'book_name'=>'교재명', 'tel2'=>'핸드폰');

if(isset($_GET['type']) && isset($_GET['keyword'])) { //조건 설정부분
    $type = $_GET['type'];
    $keyword = $_GET['keyword'];
    $_sqlWhere = _makeSqlWhere($_sqlWhere, $type, $keyword);
}

foreach($statusKeyArray as $key=>$val) {
    $_sqlWhereTemp = _makeSqlWhere($_sqlWhere, 'status', $val); 
    $_res = sql_fetch("SELECT count(no) as cnt FROM book_order".$_sqlWhereTemp);
    $_tplSetting[$key] = $_res['cnt']; 
}
if(isset($_GET['status'])) { 
    $status = $_GET['status']; 
    $_sqlWhere = _makeSqlWhere($_sqlWhere, 'status', $_GET['status']);
}

$nowPage = isset($_GET['page']) ? $_GET['page'] : 1;  // 페이징 세팅부분
$perPage = 10; 
$_res = sql_fetch("SELECT count(no) as cnt FROM book_order".$_sqlWhere);
$totalPage = ceil($_res['cnt']/$perPage);
if(( ($nowPage > $totalPage) && $nowPage > 1) || ($nowPage < 1)) {
    alert("페이지 번호가 잘못되었습니다.");
}

$_sqlLimitPage = ($nowPage-1)*$perPage; 
$_sqlOrder = " ORDER BY no DESC"; 
$_sqlLimit = " LIMIT $_sqlLimitPage, $perPage"; 
$sql = 'SELECT * FROM book_order'.$_sqlWhere.$_sqlOrder.$_sqlLimit;
$_res = sql_query($sql); 

require_once ('./admin.head.php');

$_res = sql_query($sql);

while($_ret = sql_fetch_array($_res)) {
    $_ret['selectbox'] = _makeOptionList($statusArray, $_ret['status']); // 상태 셀렉트박스 각각 설졍
    $_ret['memo']=htmlspecialchars(stripslashes($_ret['memo'])); 
    $data[] = $_ret; 
}

if(!$data) { //데이터가 없으면 데이터 없음 표시
    $_tplSetting['nodata'] = TRUE; 
}

$_tplSetting['paging'] = get_paging($perPage,$nowPage,$totalPage,"./bookOrders.php?page=",makelink(array('page')));
//페이징에서는 status를 GET으로 넘기다가
$_tplSetting['makelink'] = makelink(array('status','page'));
//상단 링크에는 status값이 필요없음
$_tplSetting['status'] = $status;
if($type && $keyword) {
    $_tplSetting['searchKeyword'] = $keyword; 
    $_type = $type; 
}
$_tplSetting['searchType'] = _makeOptionList($searchOptionArray, $_type);

$tpl->assign('data', $data);
$tpl->assign('set', $_tplSetting);
$tpl->define('basic',"bookOrders.html");
$tpl->print_('basic');

require_once('admin.foot.php');
